$(document).ready(function(){

//导航栏————————————————
function qiehuan(){
	$("#unavicon").slideToggle();
	//$("#unavitog").slideToggle();
};

$("#unavitog").click(function(){
qiehuan();
});

$("#unavitogl").click(function(){
qiehuan();
});

//字体大小———————————————
var winwid=$(window).width();
var winhei=$(window).height();
var calcuw=winwid/12;
var calcuh=winhei/6;
if (calcuh<calcuw) calcuw=calcuh;
$("div#main").css("font-size",calcuw);



//数据json（需要php）————————————————
var jsondata = jQuery.parseJSON( fromphp[0] );  

var mx1,my1,mx2,my2; //初始化坐标存储


//主处理函数—————————————————————
function ledc(j,dou,mx,my) {
	
	onoff=0;
	
	if(dou==0) { //console.log('138');
		mx1=mx;
		my1=my;
		//if ($.trim(window.getSelection().toString())) mx1=null;
	};

	if(dou==1) { //console.log('143');
		mx2=mx;
		my2=my;
		
		onoff=1;
		if(mx1==null) onoff=0;
		if(my1==null) onoff=0;
		if(Math.abs(mx2-mx1)>10) onoff=0;
		if(Math.abs(my2-my1)>10) onoff=0;
		
	};

	if(dou==2) { //console.log('154');
		onoff=1;
	};
	
	
	if(onoff==1) {
	mx1=null; mx2=null; my1=null; my2=null;
	x=x+j;
	
	if(x<0) x=0;
	if(x>=jsondata.length) {
		x=jsondata.length-1;
		alert('到头啦~！先休息一下吧！');
	};
	
	$("div#main span#mainled").html(jsondata[x]);
	window.history.pushState(null, null, 'http://'+fromphp[1]+'?book='+fromphp[2]+'&x='+x);
	};
};


//事件绑定发动函数————————————————————
var x=fromphp[3];  //初始跳
ledc(0,2,null,null);

var selectedT;
$("div#main").mousedown(function(ed){  //按下鼠标按键
	selectedT=$.trim(window.getSelection().toString());
	if (!selectedT)	ledc(0,0,ed.pageX,ed.pageY);
});

$("div#main").mouseup(function(eu){  //放开鼠标按键
	selectedT=$.trim(window.getSelection().toString());
	if (eu.which==1) {
		if(!selectedT) ledc(1,1,eu.pageX,eu.pageY);
	}
	else if (eu.which==3) {
		if(!selectedT) ledc(-1,2,null,null);
	};
});

function stopmigi(){
if(!selectedT) return false;
}
document.oncontextmenu=stopmigi;


$("div#back").click(function(){  //返回
	ledc(-1,2,null,null);
});


//功能区按钮————————————————————————

var buttonSs=1;		//文字选择开关
$("button#selectsw").click(function(){
	buttonSs=(buttonSs==1)?0:1;
	
	onselectstart1=(buttonSs==1)?null:"return false;";
	$("div#main").attr("onselectstart",onselectstart1);
	moz1=(buttonSs==1)?null:"none";
	$("div#main").css("-moz-user-select",moz1);
	
	bu1text=(buttonSs==1)?"文字选择：开":"文字选择：关";
	$(this).text(bu1text);
});


//高度调整
$("button#taller").click(function(){
	$("div#main").height($("div#main").height()+20);
});

$("button#shorter").click(function(){
	$("div#main").height($("div#main").height()-20);
});

//字号调整
$("button#bigger").click(function(){
	zihao=parseInt($("div#main").css("font-size"));
	zihao=zihao+4;
	$("div#main").css("font-size",zihao);
});

$("button#smaller").click(function(){
	zihao=parseInt($("div#main").css("font-size"));
	zihao=zihao-4;
	$("div#main").css("font-size",zihao);
});

//夜间模式
var night1=0;
$("button#night").click(function(){
	night1=(night1==1)?0:1;
	
	backc=(night1==0)?"Yellow":"#000";
	textc=(night1==0)?"#000":"#FFF";
	unavic=(night1==0)?"#FFCCBC":"#BD2D30";
	
	$("div#main").css({
		"background-color":backc,
		"color":textc
	});
	$("body").css({
		"background-color":backc
	});
	$("div#unavitog").css({
		"background-color":unavic,
		"color":textc
	});
});




});